package com.aauedu.auth;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@SpringBootTest
public class Java8 {

    //新特性1：方法引用
    @Test
    public void test1() {
        //类型1：构造器引用
        final Car car = Car.create( Car::new );
        final List< Car > cars = Arrays.asList( car );
        //类型2：静态方法引用
        cars.forEach(Car::collide);
        //类型3：特定类的任意对象的方法引用
        cars.forEach(Car::repair);
        //类型4：特定对象的方法引用
        final Car police = Car.create( Car::new );
        cars.forEach( police::follow );
        List<String> names = new ArrayList();

        names.add("Google");
        names.add("Runoob");
        names.add("Taobao");
        names.add("Baidu");
        names.add("Sina");

        names.forEach(System.out::println);
    }
}

@FunctionalInterface
interface Supplier<T> {
    T get();
}

class Car {
    //Supplier是jdk1.8的接口，这里和lamda一起使用了
    public static Car create(final Supplier<Car> supplier) {
        return supplier.get();
    }

    public static void collide(final Car car) {
        System.out.println("Collided " + car.toString());
    }

    public void follow(final Car another) {
        System.out.println("Following the " + another.toString());
    }

    public void repair() {
        System.out.println("Repaired " + this.toString());
    }
}
